package com.wengu.mythread;

import cn.hutool.core.util.RandomUtil;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author wanghailei
 * @date 2023/02/27
 */
@NoArgsConstructor
@AllArgsConstructor
public class MyCallable1 implements Callable<Integer> {
    private int i;

    @Override
    public Integer call() throws Exception {
        System.out.println(new Date()+"--"+Thread.currentThread().getName()+"--This is MyCallable...begin:"+i);
        try {
            Thread.sleep(3000+ RandomUtil.randomInt(500,1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(new Date()+"--"+Thread.currentThread().getName()+"--This is MyCallable...end:"+i);
        return i;
    }

    public static void main(String[] args) {
        System.err.println(new Date() + "--This is main.........................begin");
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 4, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(3));
        List<Future<Integer>> list = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            MyCallable1 myCallable = new MyCallable1(i);
            Future<Integer> future = executor.submit(myCallable);
            list.add(future);
        }
//        for (Future<Integer> future : list) {
//            Integer result = null;
//            try {
//                result = future.get();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            System.err.println(new Date() + "--future result="+ result);
//        }
        executor.shutdown();
        System.err.println(new Date() + "--This is main.........................end");
    }
}
